package Model.Visible;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import Controller.Game.GamePanelController;
import Model.Player.Player;
import Model.Stances.Defenses;
import Model.Stances.FleetMaker;
import Model.Stances.Industry;
import Utils.ConstantsImplement;
import Utils.Timers.Timer;
import Utils.Timers.TimerListener;
import View.GUI.FleetGUI;
import View.GUI.PlanetGUI;


public class Planet extends VisibleElements implements TimerListener, ConstantsImplement{
	private int hash_;
	private Player owner_;
	private int resources_;
	private FleetMaker fleetMaker_;
	private Industry industry_;
	private Defenses defences_;
	private Timer timer_;
	private int shipsToCreate_;
	private int population_;
	private int resourcesSpeed_;
	private boolean underAttack_;
	private int resourcesIncrement;
	private Fleet fleet_;
	private Ship attakers_[];
	private GamePanelController gamePanelController_;
	private ArrayList<Fleet> fleetsAttacking_;
	private int numFleets;


//	public Planet( String name, Dimension size, BufferedImage image, Point position){
//		super( position );
//		id_ = 0;
//		resources_ = 0;
//		population_ = 0;
//		resourcesSpeed_ = 0;
//		shipsToCreate_ = 0;
//		//fleet_ = new Fleet(owner_, gamePanelController_);
//		underAttack_ = false;
//		fleetsAttacking_ = new ArrayList<Fleet>();
//		timer_ = new Timer(this, INCREMENT_TIME);
//		timer_.start();
//	}
	public Planet(int hash, Point point, GamePanelController gamePanelController) {
		// TODO Auto-generated constructor stub
		owner_ = null;
		hash_ = hash;
		position_ = point;
		resources_ = 0;
		population_ = 0;
		resourcesSpeed_ = 0;
		shipsToCreate_ = 0;
		gamePanelController_=gamePanelController;
		fleet_ = new Fleet(owner_, gamePanelController_);
		underAttack_ = false;
		timer_ = new Timer(this, INCREMENT_TIME);
	}
	
	public int getResources_() {
		return resources_;
	}
	public void setResources_(int resources_) {
		this.resources_ = resources_;
	}
	public FleetMaker getFleetMaker_() {
		return fleetMaker_;
	}
	public void setFleetMaker_(FleetMaker fleetMaker_) {
		this.fleetMaker_ = fleetMaker_;
	}
	public Industry getIndustry_() {
		return industry_;
	}
	public void setIndustry_(Industry industry_) {
		this.industry_ = industry_;
	}
	public int getPopulation_() {
		return population_;
	}
	public void setPopulation_(int population_) {
		this.population_ = population_;
		//System.out.println("Nova Populacao: " + this.population_);
	}
	public Fleet getFleet_() {
		return fleet_;
	}
	public void setFleet_(Fleet fleet_) {
		this.fleet_ = fleet_;
	}
	
	public Player getOwner(){
		return owner_;
	}
	
	public void setOwner(Player player){
		owner_ = player;
		fleet_.setOwner_(player);
	}
	
	private void updateResources(){
		resources_ += resourcesIncrement;
		//resources += Industry.generateResources;

	}

	public int getResources(){
		return resources_;
	}
	public boolean NETcreateIndustry(){
		if(resources_ >= INITIAL_INDUSTRY_PRICE){
			industry_ = new Industry(this);
			resources_ -= industry_.getPrice();
			return true;
		}
		return false;
	}
	public boolean createIndustry(){
		if(resources_ >= INITIAL_INDUSTRY_PRICE){
			gamePanelController_.getClient_().sendMessage("CREATEI "+ hash_);
			industry_ = new Industry(this);
			resources_ -= industry_.getPrice();
			return true;
		}
		return false;
	}
	public boolean NETupdateIndustry(){
		if( this.resources_ >= this.industry_.getPrice() ){
			this.industry_.updateStance();
			return true;
		}
		return false;
	}
	public boolean updateIndustry(){
		if( this.resources_ >= this.industry_.getPrice() ){
			gamePanelController_.getClient_().sendMessage("UPDATEI "+ hash_);
			this.industry_.updateStance();
			return true;
		}
		return false;
	}
	public boolean NETcreateFleetMaker(){
		if (resources_ >= INITIAL_FLEET_MAKER_PRICE){
			fleetMaker_ = new FleetMaker(this);
			resources_ -= fleetMaker_.getPrice();
			return true;
		}
		return false;
	}
	public boolean createFleetMaker(){
		if (resources_ >= INITIAL_FLEET_MAKER_PRICE){
			gamePanelController_.getClient_().sendMessage("CREATEFM "+ hash_);
			fleetMaker_ = new FleetMaker(this);
			resources_ -= fleetMaker_.getPrice();
			return true;
		}
		return false;
	}
	public boolean NETupdateFleetMaker(){
		if( this.resources_ >= this.fleetMaker_.getPrice() ){
			this.fleetMaker_.updateStance();
			return true;
		}
		return false;
	}
	public boolean updateFleetMaker(){
		if( this.resources_ >= this.fleetMaker_.getPrice() ){
			gamePanelController_.getClient_().sendMessage("UPDATEFM "+ hash_);
			this.fleetMaker_.updateStance();
			return true;
		}
		return false;
	}
	public boolean NETcreateDefences(){
		if(resources_ >= INITIAL_DEFENSES_PRICE){
			defences_ = new Defenses(this);
			resources_ -= defences_.getPrice();
			return true;
		}
		return false;
	}
	public boolean createDefences(){
		if(resources_ >= INITIAL_DEFENSES_PRICE){
			gamePanelController_.getClient_().sendMessage("CREATED "+ hash_);
			defences_ = new Defenses(this);
			resources_ -= defences_.getPrice();
			return true;
		}
		return false;
	}
	public boolean NETupdateDefenses(){
		if( this.resources_ >= this.defences_.getPrice() ){
			this.defences_.updateStance();
			return true;
		}
		return false;
	}
	public boolean updateDefenses(){
		if( this.resources_ >= this.defences_.getPrice() ){
			gamePanelController_.getClient_().sendMessage("UPDATED "+ hash_);
			this.defences_.updateStance();
			return true;
		}
		return false;
	}
	/*	-- No more Used --
 * 	public void sellStance(){
		///Stance stance
		 resources += Defences.getSellValue;
		 Problemas;;;Como saber o tipo da stance
		 
	}
	public void killPeople(){
		for (int i = 0; i < attakers_.length; i++) {
			population_ -= attakers_[i].getAttack_();
			if(population_ <= 0){
				player_ = attakers_[i].getPlayer_();
				break;
			}
		}
		this.rewind();
	}
	*/ 
	/**
	 * to do ---
	 */
	public void rewind (){

	}
//	public void attack( ){
//		int damage = defences_.getAttack_() / numFleets;
//		for (int i = 0; i < numFleets; i++) {
//			fleetsAttacking_.get(i).distribAttack(damage);
//		}
//	}
	public Defenses getDefences_() {
		return defences_;
	}
	public void setDefences_(Defenses defences_) {
		this.defences_ = defences_;
	}
	public void update() {
		// TODO Auto-generated method stub
		population_+= INCREMENT_POPULATION;
		resources_ += INCREMENT;//*population_;
	}
	public void addResource(int i){
		resources_ = resources_+i;
	}
	public int getShipsToCreate_() {
		return shipsToCreate_;
	}
	public void setShipsToCreate_(int shipsToCreate_) {
		this.shipsToCreate_ = shipsToCreate_;
	}
	public void increaseShipsToCreate_() {
		this.shipsToCreate_++;
	}
	public void decreaseShipsToCreate_() {
		this.shipsToCreate_--;
	}
	public void NETcreateFleet(){
		if (resources_ >= fleetMaker_.getShipPrice_()*shipsToCreate_){
			fleetMaker_.construct(shipsToCreate_, this);
			resources_ -= fleetMaker_.getShipPrice_()*shipsToCreate_;
		}
	}
	public void createFleet(){
		if (resources_ >= fleetMaker_.getShipPrice_()*shipsToCreate_){
			gamePanelController_.getClient_().sendMessage("CREATEF "+ hash_ + " " + shipsToCreate_);
			fleetMaker_.construct(shipsToCreate_, this);
			resources_ -= fleetMaker_.getShipPrice_()*shipsToCreate_;
		}
	}
	
	public void setUnderAttack_(boolean underAttack) {
		underAttack_ = underAttack;
	}
	
	public ArrayList<Fleet> getFleetsAttacking_() {
		return fleetsAttacking_;
	}
	
	public int getNumFleets() {
		return numFleets;
	}
	
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return hash_;
	}
	
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return (obj.hashCode() == hash_);
	}
	
	public void startTimer(){
		timer_.start();
	}
	
	public void stopTimer(){
		timer_.setRun_(false);
	}
	
	public void attackPlanet(Planet planet_def, int numships){
		Fleet attackFleet = fleet_.divideFleet(numships, this);
		attackFleet.setPlanetToAttack(planet_def);
		attackFleet.setMoving_(true);
		FleetGUI fleetGui = new FleetGUI(attackFleet, gamePanelController_);
		attackFleet.setFleetGUI_(fleetGui);
		gamePanelController_.addFleetGUI(fleetGui);
		gamePanelController_.getUniverse_().addFleet(attackFleet);
		
	}
}
